package com.xiaodu.acer.phoneplay;

import android.app.Application;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;
import android.view.View;

import com.mikepenz.iconics.Iconics;
import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.Logger;
import com.xiaodu.acer.phoneplay.bean.DaoMaster;
import com.xiaodu.acer.phoneplay.bean.DaoSession;
import com.xiaodu.acer.phoneplay.bean.downloadbean.DownloadSqliteActor;
import com.xiaodu.acer.phoneplay.common.Constant;
import com.xiaodu.acer.phoneplay.common.font.CustomFont;
import com.xiaodu.acer.phoneplay.di.component.AppComponent;
import com.xiaodu.acer.phoneplay.di.component.DaggerAppComponent;
import com.xiaodu.acer.phoneplay.di.module.AppModule;
import com.xiaodu.acer.phoneplay.di.module.HttpModule;

import java.io.File;

import library.util.ACache;
import zlc.season.rxdownload3.core.DownloadConfig;
import zlc.season.rxdownload3.extension.ApkInstallExtension;
import zlc.season.rxdownload3.extension.ApkOpenExtension;
import zlc.season.rxdownload3.http.OkHttpClientFactoryImpl;
import zlc.season.rxdownload3.notification.NotificationFactoryImpl;

import static android.os.Environment.DIRECTORY_DOWNLOADS;


/**
 * @author 小杜 QQ:1392229149
 * @name phone
 * @class name：com.xiaodu.acer.phoneplay
 * @class describe
 * @time 2017/12/17 11:30
 * @change
 * @chang time
 * @class describe
 */
public class App extends Application {

    private static DaoSession daoSession;
    private View mView;

    private AppComponent mAppComponent;

    public View getView() {
        return mView;
    }

    public void setView(View view) {
        mView = view;
    }

    public static App get(Context context) {
        return (App) context.getApplicationContext();
    }

    public AppComponent getAppComponent() {
        return mAppComponent;
    }


    @Override
    public void onCreate() {
        super.onCreate();


        Iconics.init(getApplicationContext());
        Iconics.registerFont(new CustomFont());


        Logger.addLogAdapter(new AndroidLogAdapter() {
            @Override
            public boolean isLoggable(int priority, String tag) {
                return BuildConfig.DEBUG;
            }
        });

        mAppComponent = DaggerAppComponent.builder()
                .appModule(new AppModule(this))
                .httpModule(new HttpModule())
                .build();

        //保存下载路径
        File file = Environment.getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS);
        ACache.get(this).put(Constant.APK_DOWNLOAD_DIR, file.getPath());
        DownloadConfig.Builder builder = DownloadConfig.Builder
                .Companion.create(this)
                .setDefaultPath(file.getPath())
                .setDebug(true)
                .enableDb(true)     //设置数据库可用
                .setDbActor(new DownloadSqliteActor(this))  //使用自定义数据库
                .enableService(true)
                .addExtension(ApkInstallExtension.class)
                .addExtension(ApkOpenExtension.class)
                .enableNotification(true)                   //启用Notification
                .setNotificationFactory(new NotificationFactoryImpl())        //自定义通知
                .setOkHttpClientFacotry(new OkHttpClientFactoryImpl());        //自定义OKHTTP;


        DownloadConfig.INSTANCE.init(builder);

        //配置数据库
        setupDatabase();
    }


    /*
      * 配置数据库
      */
    private void setupDatabase() {
        //创建数据库phone.db"
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "phone.db", null);
        //获取可写数据库
        SQLiteDatabase db = helper.getWritableDatabase();
        //获取数据库对象
        DaoMaster daoMaster = new DaoMaster(db);
        //获取Dao对象管理者
        daoSession = daoMaster.newSession();
    }

    public static DaoSession getDaoInstant() {
        return daoSession;
    }
}
